//
//  YKClientOrderDetailViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/2.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKClientOrderDetailViewController.h"
#import "YKOrderDetailHeaderView.h"
#import "YKOrderDetailListHeaderView.h"
#import "YKOrderDetailItem.h"
#import "YKPopupView.h"
#import "YKProcessTrackingViewController.h"
#import "YKAuditSuggestViewController.h"
#import "YKTurnOverViewController.h"
#import "YKApproveBottomView.h"
#import "WorkFlowManager.h"
#import "WorkFlowBottomView.h"

@interface YKClientOrderDetailViewController ()
@property (nonatomic, strong) YKOrderDetailModel *mYKOrderDetailModel;
@property (nonatomic, strong) UIButton* repealOrderButton;
@property (nonatomic, strong) UIButton *repositionOrderButton;
@property (nonatomic, strong) UIButton *confirmButton;
@property (nonatomic, strong) WorkFlowBottomView *bottomView;
@end

@implementation YKClientOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"订单详情";
    UIView *header = [[UIView alloc]init];
    
    
    YKOrderDetailHeaderView *headerView = [[NSBundle mainBundle]loadNibNamed:@"YKOrderDetailHeaderView" owner:self options:nil].firstObject;
    [header addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(header);
    }];
    
    YKOrderDetailListHeaderView *listHeaderView = [[NSBundle mainBundle]loadNibNamed:@"YKOrderDetailListHeaderView" owner:self options:nil].firstObject;
    [header addSubview:listHeaderView];
    [listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(header);
        make.top.equalTo(headerView.mas_bottom);
        make.bottom.equalTo(header.mas_bottom);
    }];
    
    CGSize size = [header systemLayoutSizeFittingSize:CGSizeMake(self.view.tf_width, CGFLOAT_MAX)];
    header.frame = CGRectMake(0, 0, size.width, size.height);
    self.tableView.tableHeaderView = header;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    

    UIButton *confirmButton = [[UIButton alloc]init];
    //暂时屏蔽提交审批功能
//    [self.view addSubview:confirmButton];

    [confirmButton setTitle:@"确认审批" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:[UIColor tf_colorWithHexString:@"#f1f1f1"]];

    self.confirmButton = confirmButton;


    [confirmButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor tf_colorWithHexString:@"f1f1f1"]] forState:UIControlStateNormal];//
    [confirmButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor grayColor]] forState:UIControlStateDisabled];//
    [confirmButton setTitleColor:[UIColor tf_colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
    confirmButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [confirmButton addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
//        make.top.equalTo(headerView.mas_bottom);
        make.bottom.equalTo(self.view.mas_bottom).offset(-44);
    }];
    
    [self tf_showStateView:kTFViewStateLoading];
    NSString *url = @"customerOrder/searchOrderInfoById.htm";
    if ([self.status isEqualToString:@"9"] || [self.status isEqualToString:@"10"]) {
        url = @"customerOrder/searchReturnOrderByOrderId.htm";
        self.title = @"退货详情";
    }
    [TFHTTPMANAER POST:url parameters:@{@"id":self.ID} success:^(id responseObject) {
        self.mYKOrderDetailModel = [YKOrderDetailModel yy_modelWithJSON:responseObject];

        headerView.mYKOrderDetailModel = self.mYKOrderDetailModel;
        listHeaderView.ZSUM.text = [NSString stringWithFormat:@"%.2f", self.mYKOrderDetailModel.customerDataMap.RECEIVABLE_AMOUNT];
        listHeaderView.CANCELSUM.text = [NSString stringWithFormat:@"%.2f", self.mYKOrderDetailModel.customerDataMap.ACCOUNT_PAYMENT];
        
        
        
        [self tf_showStateView:kTFViewStateNone];

        if ([self.mYKOrderDetailModel.orderMap.ISTATUS isEqualToString:@"9"]) {
            self.bottomView = [[WorkFlowBottomView alloc] initWithType:WorkFlowBottomViewTypeRefunding];
        }
        else if ([self.mYKOrderDetailModel.orderMap.ISTATUS isEqualToString:@"10"]) {
            self.bottomView = [[WorkFlowBottomView alloc] initWithType:WorkFlowBottomViewTypeRefunded];
        }
        else {
            self.bottomView = [[WorkFlowBottomView alloc] initWithType:WorkFlowBottomViewTypeNormal];
        }
        self.bottomView.ID = self.ID;
        self.bottomView.ROID = self.mYKOrderDetailModel.orderMap.ROID;
        self.bottomView.RO_BUSINESS_EXAMINESTATUS = self.mYKOrderDetailModel.orderMap.RO_BUSINESS_EXAMINESTATUS;

        [self.view addSubview:self.bottomView];
        [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.bottom.trailing.equalTo(self.view);
        }];
        
        [self updateViews];
        self.bottomView.BUSINESSEXAMINESTATUS = self.mYKOrderDetailModel.orderMap.BUSINESSEXAMINESTATUS;

        if (self.approveStatus) {
            [YKApproveBottomView addSelfToView:self ID:self.ID  approveStatus:self.approveStatus];
        }
        
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError];
    }];
    
    
    
}


- (void)updateViews {
    TFTableViewSection* section = [TFTableViewSection section];
    section.headerHeight = .1f;
    section.footerHeight = .1f;
    //    @weakify(self)
    for (int i = 0; i < self.mYKOrderDetailModel.orderGoosList.count; ++i) {
        YKOrderDetailItem* item = [YKOrderDetailItem itemWithModel:self.mYKOrderDetailModel.orderGoosList[i]];
        item.istatus = self.status;
        [section addItem:item];
        item.hideSeparator = YES;
        item.selectionHandler = ^(__kindof TFTableViewItem *item, NSIndexPath *indexPath) {
            
        };
    }
    
    [self.tableViewManager addSection:section];
    [self.tableView reloadData];
}


- (void)processTrack:(id)sender {
    YKProcessTrackingViewController *vc = [YKProcessTrackingViewController new];
    vc.m_id = self.ID;
    [YKPopupView showPopupViewWithViewController:vc];
}


/**
 确认审批
 */
- (void)confirm:(id)sender {
    [TFHTTPMANAER POST:@"customerOrder/updateOrderInfoById.htm"
            parameters:@{@"orderId":self.ID,@"examinestatus":@"2",@"orderreturnreason":@""}
                   HUD:@"确认审批中..."
               success:^(id responseObject) {
        [self showHint:@"确认审批成功"];
                   [self.navigationController popViewControllerAnimated:YES];
    }
               failure:^(id responseObject, NSError *error) {
        [self showHint:error.localizedDescription];
    }];
}
@end
